package com.cjjd.c.feature.home.hoteldevice;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.PermissionChecker;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.cjjd.c.R;
import com.cjjd.c.base.ActionBarFragment;
import com.cjjd.c.bean.HotelFacilityInfoModel;
import com.cjjd.c.utils.ImageUtil;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/10/15.
 */

public class HomeHotelDeviceDetailFragment extends ActionBarFragment {
    @Bind(R.id.icon)
    ImageView icon;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.start_time)
    TextView startTime;
    @Bind(R.id.end_time)
    TextView endTime;
    @Bind(R.id.jieshao)
    TextView jieshao;
    @Bind(R.id.dec)
    TextView dec;
    @Bind(R.id.send)
    Button send;


    HotelFacilityInfoModel mBean;
    @Override
    public Object getTitleString() {
        return "设施";
    }

    @Override
    public int getContentView() {
        return R.layout.fragment_chess_room;
    }


    private String id;

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        mBean=(HotelFacilityInfoModel)getArguments().getSerializable("bean");

        mActionBar.setTitle(mBean.typeName);
        ImageUtil.displayImage(mBean.iconUrl,icon);
        tvTitle.setText(mBean.hotelFacilityName);
        startTime.setText("开放时间："+mBean.openHour);
        dec.setText(mBean.description);

        if(TextUtils.isEmpty(mBean.phone)){
            send.setVisibility(View.GONE);
        }else{
            send.setVisibility(View.VISIBLE);
        }


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick(R.id.send)
    public void onViewClicked() {



        if (Build.VERSION.SDK_INT >= 23) {

            //判断有没有拨打电话权限
            if (PermissionChecker.checkSelfPermission(mContext, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {

                //请求拨打电话权限
                ActivityCompat.requestPermissions(mContext, new String[]{Manifest.permission.CALL_PHONE}, REQUEST_CODE);

            } else {
                call();
            }

        } else {
            call();
        }
    }
    private final int REQUEST_CODE = 0x1001;
    /**
     * 请求权限的回调方法
     * @param requestCode    请求码
     * @param permissions    请求的权限
     * @param grantResults   权限的结果
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if (requestCode == REQUEST_CODE && PermissionChecker.checkSelfPermission(mContext, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
            call();
        }
    }
    private void call(){

        try {
            Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+mBean.phone));
            mContext.startActivity(intent);
        }catch (SecurityException e){
        }

    }
}
